package com.ls.fw.data.search.impl.solrj.statement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.common.SolrInputDocument;

import com.ls.fw.data.bean.Column;
import com.ls.fw.data.bean.Record;
import com.ls.fw.data.statement.Update;
import com.ls.fw.data.visitor.FieldVisitor;
import com.ls.fw.data.visitor.RecordVisitor;
import com.ls.fw.data.visitor.UpdateVisitor;

public class UpdateParser extends BaseParser implements RecordVisitor, FieldVisitor, UpdateVisitor {

	private Collection<SolrInputDocument> docs;
	private Update update = null;
	public UpdateParser(Update update) {
		super();
		this.update  = update;
		this.visit(update);
	}

	@Override
	public void visit(Update update) {
		this.docs = new ArrayList<SolrInputDocument>(update.getRecords().size());
		update.accept(this);
		WhereParser w = new WhereParser(update.getTable(),update.getWhere());
		w.parse(query);
	}

	@Override
	public void visit(Record record) {
		SolrInputDocument doc = new SolrInputDocument();
		List<Column> columns = record.getFields();
		for (Column column : columns) {
			doc.addField(column.getName(), column.getValue(), column.getBoost());
		}
		docs.add(doc);
	}
	
	@Override
	public void visit(Column column) {
		// TODO Auto-generated method stub
		
	}

	public Collection<SolrInputDocument> getDocs() {
		return docs;
	}

}
